package com.yimi.yinhepda.views.load_module;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.pda.ym.base.BaseActivity;
import com.yimi.yinhepda.R;
import com.pda.ym.callbacks.UICallBack;
import com.yimi.yinhepda.db.dao.UnloadScanTableDao;
import com.yimi.yinhepda.entity.response.QueryCreateTaskDictResponse;
import com.pda.ym.net.NetRequest;
import com.pda.ym.utils.Constants;
import com.pda.ym.utils.FastClick;

import java.util.HashMap;
import java.util.Map;

/**
 * 出发装车界面
 *
 * @author 000432
 */
public class LoadFunctionActivity extends BaseActivity {
    private TextView num;
    private UnloadScanTableDao mUnloadScanTableDao;
    private Button btnCreateLoadTask;
    private Button mBtnLoadTaskList;
    private Button mBtnSealCarList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load_function);
        initView();
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //设置未上传数
        num.setText((int) mUnloadScanTableDao.queryCount("isUpload", 0, 1) + "");
    }

    private void initView() {
        ((TextView) findViewById(R.id.title)).setText("出发装车");
        num = findViewById(R.id.num);
        mBtnLoadTaskList = findViewById(R.id.load_task_btn);
        mBtnLoadTaskList.setOnClickListener(this);
        mBtnSealCarList = findViewById(R.id.btn_sealCarList);
        mBtnSealCarList.setOnClickListener(this);
        findViewById(R.id.unload_task_back_btn).setOnClickListener(this);
        btnCreateLoadTask = findViewById(R.id.btn_create_load_task);
        btnCreateLoadTask.setOnClickListener(this);
    }

    private void initData() {
        mUnloadScanTableDao = new UnloadScanTableDao();
        btnCreateLoadTask.setVisibility(View.GONE);
        queryDictValue();
    }

    @Override
    public String getPageName() {
        return "出发装车";
    }

    /**
     * 查询数据字典，判断该省区是否开启创建配载单功能
     */
    private void queryDictValue() {
        showPgDlg("正在查询配置...");
        Map<String, String> params = new HashMap<>();
        //托盘解绑数据字典字段
        params.put("params", "establish_task");
        new NetRequest().setMethod(NetRequest.Method.GET)
                .setUrl(Constants.URL_QUERY_DICT_VALUE)
                .setParams(params)
                .setConnTimeOut(5 * 1000)
                .setResponsClazz(QueryCreateTaskDictResponse.class)
                .execute(new UICallBack() {
                    @Override
                    public void onError(String msg) {
                        dismissPgDlg();
                        showToast(msg);
                    }

                    @Override
                    public void onSuccess(Object obj) {
                        dismissPgDlg();
                        QueryCreateTaskDictResponse response = (QueryCreateTaskDictResponse) obj;
                        if (response != null) {
                            if (response.getData() != null) {
                                Map<String, String> maps = response.getData().getCreateTaks();
                                if (maps != null) {
                                    for (String value : maps.values()) {
                                        //判断是否需要逐票解绑
                                        if (value.equals(sharedPreferUtil.getCompanyCode())) {
                                            btnCreateLoadTask.setVisibility(View.VISIBLE);
                                            mBtnLoadTaskList.setText("2.装车任务列表");
                                            mBtnSealCarList.setText("3.出发封车列表");
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        if (FastClick.isFastClick()) {
            return;
        }

        switch (v.getId()) {
            case R.id.btn_create_load_task:
                //创建装车任务
                toActivity(CreateLoadTaskActivity.class);
                break;
            case R.id.load_task_btn:
                //装车任务列表
                if (sharedPreferUtil.getOfflineMode()) {
                    showToast(getString(R.string.no_use_in_offline));
                    return;
                }
                toActivity(LoadTaskActivity.class);
                break;
            case R.id.btn_sealCarList:
                //出发封车界面
                toActivity(SetOutSealCarActivity.class);
                break;
            case R.id.unload_task_back_btn:
                finish();
                break;
            default:
                break;
        }
    }
}
